package com.fy56.platform.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.Company;
import com.fy56.platform.logistics.model.News;
import com.fy56.platform.logistics.service.web.INewsService;

import com.fy56.platform.logistics.service.wx.ICompanyInfoService;
import com.fy56.platform.logistics.utils.LogUtils;
import com.fy56.platform.logistics.utils.SessionUtil;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 新聞消息controller
 * Created by chen on 2018-05-10.
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/controller/news")
public class NewsController extends BaseController {

    @Autowired
    private INewsService iNewsService;
    @Autowired
    private ICompanyInfoService iCompanyInfoService;

    /**
     * 获取新聞消息信息
     *
     * @param keyId
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ResponseBody
    public News getNews(Long keyId, HttpServletRequest request){
        News news = new News();
        if(keyId != null){
            news = iNewsService.selectByPrimaryKey(keyId);
        }
        /*String code=news.getCode().toString();
        Company company=iCompanyInfoService.getCompanyByCode(code);
        news.setCode(company.getName());*/
        return news;
    }

    /**
     * 添加保存
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(News news, HttpServletRequest request) {
        ResultModel resultModel = null;
        List<News> searchList = null;
        try {
            if (news != null) {
                //执行插入
                news.initInsert(SessionUtil.getSessionUser(request));
                resultModel = iNewsService.insert(news);

                //保存系统业务日志
                LogUtils.saveLog(request, "成功", news.getId().toString(), null, "newsManger", "save", "添加", JSON.toJSONString(news));
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel(false), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            String key = (news == null || news.getId() == null) ? null : news.getId().toString();
            LogUtils.saveLog(request, "失败", key, null, "newsManger", "save", "添加", JSON.toJSONString(news));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            searchList = null;
        }
    }

    /**
     * 修改保存
     *
     * @param server
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> update(News news, HttpServletRequest request) {
        ResultModel resultModel = null;
        News dbModel = null;
        try{
            if (news != null) {

                //根据主键查询DB中的记录
                dbModel = iNewsService.selectByPrimaryKey(news.getId());

                //设置修改操作员，修改时间（默认当前时间）
                news.initUpdate(SessionUtil.getSessionUser(request));

                //执行更新操作
                resultModel = iNewsService.update(news);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",news.getId().toString(),null,"newsManger","update","编辑", JSON.toJSONString(news));

                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("新聞编辑失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",news.getId().toString(),null,"newsManger","update","编辑", JSON.toJSONString(news));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            dbModel = null;
        }
    }

    /**
     * 删除
     *
     * @param keyId
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(Long keyId, HttpServletRequest request) {
        ResultModel resultModel = null;
        News news = null;
        try{
            if (keyId != null && (keyId.intValue() !=0)) {
                news = new News();
                news.setId(keyId);
                news.setDeleteFlag(1);
                news.initUpdate(SessionUtil.getSessionUser(request));

                //执行删除操作
                resultModel = iNewsService.delete(news);

                //保存系统业务日志
                LogUtils.saveLog(request,"成功",keyId.toString(),null,"newsManger","delete","删除", keyId.toString());
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("新聞删除失败!"), HttpStatus.OK);
        }catch (Exception e){

            //保存系统业务日志
            LogUtils.saveLog(request,"失败",keyId.toString(),null,"newsManger","delete","删除", keyId.toString());
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }finally {
            resultModel = null;
            news = null;
        }
    }

    /**
     * 批量删除
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> batchDelete(String keyIds, HttpServletRequest request) {
        try{
            if (StringUtil.isNotEmpty(keyIds)) {

                //构建删除对象（服务器对象的集合）
                LinkedList<News> list = new LinkedList<News>();
                News news = null;
                for(String id : keyIds.split(",")){
                    if(StringUtil.isNotEmpty(id)){
                        news = new News();
                        news.setDeleteFlag(1);
                        news.initUpdate(SessionUtil.getSessionUser(request));
                        news.setId(Long.parseLong(id));
                        list.add(news);
                    }
                }

                //执行删除操作
                ResultModel resultModel = iNewsService.batchDelete(new ArrayList<News>(list));

                //保存系统业务日志
                LogUtils.saveLog(request,"",keyIds,null,"newsManger","batchDelete","批量删除",null);
                return new ResponseEntity<ResultModel>(resultModel, HttpStatus.OK);
            }
            return new ResponseEntity<ResultModel>(new ResultModel("新闻批量删除失败!"), HttpStatus.OK);
        }catch (Exception e){
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }



}
